#!/bin/bash

# 版权和免责声明
echo "本脚本由 zjy 编写，请事先检查代码，否则本人概不负责。"
echo "注意事项："
echo "1. 本脚本可能会修改系统关键设置，请确保您了解脚本执行的操作。"
echo "2. 脚本运行过程中可能需要输入管理员密码。"
echo "3. 部分软件安装可能需要联网下载，请确保网络连接稳定。"
echo "4. 如遇问题，请联系专业技术人员协助。"
echo "5. 本脚本支持 deepin25、Fedora41、debian12、Ubuntu24.04 系统。"

# 确认用户同意
read -p "是否同意上述条款？(y/n): " agree
if [ "$agree" != "y" ] && [ "$agree" != "Y" ]; then
    echo "您未同意条款，程序退出。"
    exit 1
fi

# 识别 Linux 发行版本
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
    VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
    OS=$(lsb_release -si)
    VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    OS="debian"
    VER=$(cat /etc/debian_version)
elif [ -f /etc/redhat-release ]; then
    OS="redhat"  # 包括 CentOS、Fedora 等
else
    OS=$(uname -s)
    VER=$(uname -r)
fi

# 转换为小写
OS=$(echo "$OS" | tr '[:upper:]' '[:lower:]')

echo "检测到系统: $OS $VER"

# 重试或跳过函数
retry_skip() {
    while true; do
        read -p "操作失败，选择: 1.重试 2.跳过: " choice
        case $choice in
            1) return 0 ;;
            2) return 1 ;;
            *) echo "无效选择，请重新输入" ;;
        esac
    done
}

# 安装软件函数
install_package() {
    local package=$1
    echo "正在安装 $package..."

    if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
        sudo apt install -y "$package"
        if [ $? -ne 0 ]; then
            echo "安装 $package 失败"
            if retry_skip; then
                install_package "$package"
            fi
        fi
    elif [ "$OS" = "fedora" ]; then
        sudo dnf install -y "$package"
        if [ $? -ne 0 ]; then
            echo "安装 $package 失败"
            if retry_skip; then
                install_package "$package"
            fi
        fi
    fi
}

# 备份源列表
backup_sources() {
    echo "正在备份源列表..."
    if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
        sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
    elif [ "$OS" = "fedora" ]; then
        for repo in /etc/yum.repos.d/*.repo; do
            sudo cp "$repo" "$repo.backup"
        done
    fi
}

# 更换源
change_source() {
    backup_sources

    while true; do
        echo "请选择源:"
        echo "1. 官方源"
        echo "2. 阿里云源"
        echo "3. 网易云源"
        echo "4. 清华大学源"
        echo "5. 中科大源"
        echo "6. 搜狐源"
        echo "7. 163 源"
        echo "8. 腾讯源"
        echo "9. 华为云源"
        echo "10. 豆瓣源"
        echo "12. ustc 源（中国科学技术大学源）"
        echo "13. 阿里云镜像源"
        echo "14. 网易开源镜像站"
        echo "15. 清华镜像源"
        echo "16. 返回上一页"
        read -p "请输入选项: " source_choice

        if [ "$source_choice" = "16" ]; then
            break
        fi

        if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
            case $source_choice in
                1)
                    echo "使用官方源..."
                    # 这里需要根据不同系统添加官方源内容
                    ;;
                2)
                    echo "使用阿里云源..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                3)
                    echo "使用网易云源..."
                    # 需要添加网易云源内容
                    ;;
                4)
                    echo "使用清华大学源..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                5)
                    echo "使用中科大源..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                6)
                    echo "使用搜狐源..."
                    # 需要添加搜狐源内容
                    ;;
                7)
                    echo "使用 163 源..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
deb http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb-src http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb-src http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb-src http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
deb-src http://mirrors.163.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                8)
                    echo "使用腾讯源..."
                    # 需要添加腾讯源内容
                    ;;
                9)
                    echo "使用华为云源..."
                    # 需要添加华为云源内容
                    ;;
                10)
                    echo "使用豆瓣源..."
                    # 需要添加豆瓣源内容
                    ;;
                12)
                    echo "使用 ustc 源（中国科学技术大学源）..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                13)
                    echo "使用阿里云镜像源..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                14)
                    echo "使用网易开源镜像站..."
                    # 需要添加网易开源镜像站源内容
                    ;;
                15)
                    echo "使用清华镜像源..."
                    sudo bash -c "cat > /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs) main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ $(lsb_release -cs)-security main restricted universe multiverse
EOF"
                    ;;
                *)
                    echo "无效选项，请重新输入。"
                    continue
                    ;;
            esac

            # 更新源
            echo "正在更新源..."
            sudo apt update
            if [ $? -ne 0 ]; then
                echo "更新源失败"
                if retry_skip; then
                    sudo apt update
                fi
            fi
        elif [ "$OS" = "fedora" ]; then
            case $source_choice in
                1)
                    echo "使用官方源..."
                    # 这里需要根据 Fedora 添加官方源内容
                    ;;
                2)
                    echo "使用阿里云源..."
                    # 需要添加阿里云 Fedora 源内容
                    ;;
                3)
                    echo "使用网易云源..."
                    # 需要添加网易云 Fedora 源内容
                    ;;
                4)
                    echo "使用清华大学源..."
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.tuna.tsinghua.edu.cn/fedora|g' /etc/yum.repos.d/fedora.repo
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.tuna.tsinghua.edu.cn/fedora|g' /etc/yum.repos.d/fedora-updates.repo
                    sudo dnf makecache
                    ;;
                5)
                    echo "使用中科大源..."
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.ustc.edu.cn/fedora|g' /etc/yum.repos.d/fedora.repo
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.ustc.edu.cn/fedora|g' /etc/yum.repos.d/fedora-updates.repo
                    sudo dnf makecache
                    ;;
                6)
                    echo "使用搜狐源..."
                    # 需要添加搜狐 Fedora 源内容
                    ;;
                7)
                    echo "使用 163 源..."
                    # 需要添加 163 Fedora 源内容
                    ;;
                8)
                    echo "使用腾讯源..."
                    # 需要添加腾讯 Fedora 源内容
                    ;;
                9)
                    echo "使用华为云源..."
                    # 需要添加华为云 Fedora 源内容
                    ;;
                10)
                    echo "使用豆瓣源..."
                    # 需要添加豆瓣 Fedora 源内容
                    ;;
                12)
                    echo "使用 ustc 源（中国科学技术大学源）..."
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.ustc.edu.cn/fedora|g' /etc/yum.repos.d/fedora.repo
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.ustc.edu.cn/fedora|g' /etc/yum.repos.d/fedora-updates.repo
                    sudo dnf makecache
                    ;;
                13)
                    echo "使用阿里云镜像源..."
                    # 需要添加阿里云镜像 Fedora 源内容
                    ;;
                14)
                    echo "使用网易开源镜像站..."
                    # 需要添加网易开源镜像站 Fedora 源内容
                    ;;
                15)
                    echo "使用清华镜像源..."
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.tuna.tsinghua.edu.cn/fedora|g' /etc/yum.repos.d/fedora.repo
                    sudo sed -i 's|download.fedoraproject.org/pub|mirrors.tuna.tsinghua.edu.cn/fedora|g' /etc/yum.repos.d/fedora-updates.repo
                    sudo dnf makecache
                    ;;
                *)
                    echo "无效选项，请重新输入。"
                    continue
                    ;;
            esac

            # 更新源
            echo "正在更新源..."
            sudo dnf makecache
            if [ $? -ne 0 ]; then
                echo "更新源失败"
                if retry_skip; then
                    sudo dnf makecache
                fi
            fi
        fi
    done
}

# 打依赖与驱动
install_dependencies() {
    while true; do
        echo "打依赖与驱动:"
        echo "1. 开始"
        echo "2. 返回"
        read -p "请输入选项: " dep_choice

        case $dep_choice in
            1)
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    sudo apt update
                    sudo apt install -y build-essential linux-headers-$(uname -r)
                    if [ $? -ne 0 ]; then
                        echo "安装依赖与驱动失败"
                        if retry_skip; then
                            sudo apt update
                            sudo apt install -y build-essential linux-headers-$(uname -r)
                        fi
                    fi
                elif [ "$OS" = "fedora" ]; then
                    sudo dnf groupinstall -y "Development Tools"
                    sudo dnf install -y kernel-devel kernel-headers
                    if [ $? -ne 0 ]; then
                        echo "安装依赖与驱动失败"
                        if retry_skip; then
                            sudo dnf groupinstall -y "Development Tools"
                            sudo dnf install -y kernel-devel kernel-headers
                        fi
                    fi
                fi
                ;;
            2)
                break
                ;;
            *)
                echo "无效选项，请重新输入。"
                ;;
        esac
    done
}

# 配置开发环境
configure_dev_env() {
    while true; do
        echo "配置开发环境:"
        echo "1. 开始"
        echo "2. 返回"
        read -p "请输入选项: " dev_choice

        case $dev_choice in
            1)
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    install_package "git"
                    install_package "python3-dev"
                    install_package "python3-pip"
                elif [ "$OS" = "fedora" ]; then
                    install_package "git"
                    install_package "python3-devel"
                fi
                ;;
            2)
                break
                ;;
            *)
                echo "无效选项，请重新输入。"
                ;;
        esac
    done
}

# 安装解码器等
install_codecs() {
    while true; do
        echo "安装解码器等:"
        echo "1. 开始"
        echo "2. 返回"
        read -p "请输入选项: " codec_choice

        case $codec_choice in
            1)
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    sudo apt install -y ubuntu-restricted-extras
                    if [ $? -ne 0 ]; then
                        echo "安装解码器失败"
                        if retry_skip; then
                            sudo apt install -y ubuntu-restricted-extras
                        fi
                    fi
                elif [ "$OS" = "fedora" ]; then
                    sudo dnf install -y gstreamer1-plugins-{bad-\*,good-\*,base} gstreamer1-plugin-openh264 gstreamer1-libav --exclude=gstreamer1-plugins-bad-free-devel
                    sudo dnf install -y lame\* --exclude=lame-devel
                    sudo dnf group upgrade -y --with-optional Multimedia
                    if [ $? -ne 0 ]; then
                        echo "安装解码器失败"
                        if retry_skip; then
                            sudo dnf install -y gstreamer1-plugins-{bad-\*,good-\*,base} gstreamer1-plugin-openh264 gstreamer1-libav --exclude=gstreamer1-plugins-bad-free-devel
                            sudo dnf install -y lame\* --exclude=lame-devel
                            sudo dnf group upgrade -y --with-optional Multimedia
                        fi
                    fi
                fi
                ;;
            2)
                break
                ;;
            *)
                echo "无效选项，请重新输入。"
                ;;
        esac
    done
}

# 优化系统性能与安全设置
optimize_system() {
    while true; do
        echo "优化系统性能与安全设置:"
        echo "1. 开始"
        echo "2. 返回"
        read -p "请输入选项: " opt_choice

        case $opt_choice in
            1)
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    # 禁用 apport 错误报告
                    sudo sed -i 's/enabled=1/enabled=0/g' /etc/default/apport

                    # 调整 swappiness
                    echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
                    sudo sysctl -p

                    # 清理系统
                    sudo apt autoremove -y
                    sudo apt clean
                elif [ "$OS" = "fedora" ]; then
                    # 禁用防火墙
                    sudo systemctl disable firewalld
                    sudo systemctl stop firewalld

                    # 调整 swappiness
                    echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.conf
                    sudo sysctl -p

                    # 清理系统
                    sudo dnf clean all
                fi
                ;;
            2)
                break
                ;;
            *)
                echo "无效选项，请重新输入。"
                ;;
        esac
    done
}

# 安装如意玲珑
install_linglong() {
    if [ "$OS" = "deepin" ] && [ "$VER" = "25" ]; then
        echo "deb [trusted=yes] https://ci.deepin.com/repo/obs/linglong:/CI:/release/Deepin_25/ ./" | sudo tee /etc/apt/sources.list.d/linglong.list
        sudo apt update
        if [ $? -ne 0 ]; then
            echo "更新源失败"
            if retry_skip; then
                sudo apt update
            fi
        else
            install_package "linglong-builder"
            install_package "linglong-box"
            install_package "linglong-bin"
        fi
    elif [ "$OS" = "fedora" ] && [ "$VER" = "41" ]; then
        sudo dnf config-manager addrepo --from-repofile "https://ci.deepin.com/repo/obs/linglong:/CI:/release/Fedora_41/linglong%3ACI%3Arelease.repo"
        sudo dnf update
        if [ $? -ne 0 ]; then
            echo "更新源失败"
            if retry_skip; then
                sudo dnf update
            fi
        else
            install_package "linglong-builder"
            install_package "linglong-box"
            install_package "linglong-bin"
        fi
    elif [ "$OS" = "ubuntu" ] && [ "$VER" = "24.04" ]; then
        echo "deb [trusted=yes] https://ci.deepin.com/repo/obs/linglong:/CI:/release/xUbuntu_24.04/ ./" | sudo tee /etc/apt/sources.list.d/linglong.list
        sudo apt update
        if [ $? -ne 0 ]; then
            echo "更新源失败"
            if retry_skip; then
                sudo apt update
            fi
        else
            install_package "linglong-builder"
            install_package "linglong-box"
            install_package "linglong-bin"
        fi
    elif [ "$OS" = "debian" ] && [ "$VER" = "12" ]; then
        echo "deb [trusted=yes] https://ci.deepin.com/repo/obs/linglong:/CI:/release/Debian_12/ ./" | sudo tee /etc/apt/sources.list.d/linglong.list
        sudo apt update
        if [ $? -ne 0 ]; then
            echo "更新源失败"
            if retry_skip; then
                sudo apt update
            fi
        else
            install_package "linglong-builder"
            install_package "linglong-box"
            install_package "linglong-bin"
        fi
    fi
}

# 安装软件
install_software() {
    while true; do
        echo "安装软件:"
        echo "1. 开始"
        echo "2. 返回"
        read -p "请输入选项: " soft_choice

        case $soft_choice in
            1)
                install_package "vim"
                install_package "tmux"
                install_linglong
                install_package "rustdesk"
                install_package "wget"
                install_package "python3"
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    install_package "unzip"
                elif [ "$OS" = "fedora" ]; then
                    install_package "unzip"
                fi

                # 安装 vscode
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
                    sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
                    sudo apt update
                    install_package "code"
                elif [ "$OS" = "fedora" ]; then
                    sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
                    sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
                    install_package "code"
                fi

                # 安装 wps
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    wget -O wps-office.deb "https://wdl1.pcfg.cache.wpscdn.com/wpsdl/wpsoffice/download/linux/11664/wps-office_11.1.0.11664_amd64.deb"
                    sudo apt install -y ./wps-office.deb
                    rm wps-office.deb
                    if [ $? -ne 0 ]; then
                        echo "安装 WPS 失败"
                        if retry_skip; then
                            wget -O wps-office.deb "https://wdl1.pcfg.cache.wpscdn.com/wpsdl/wpsoffice/download/linux/11664/wps-office_11.1.0.11664_amd64.deb"
                            sudo apt install -y ./wps-office.deb
                            rm wps-office.deb
                        fi
                    fi
                elif [ "$OS" = "fedora" ]; then
                    # Fedora 可能需要其他方式安装，这里暂未实现
                    echo "WPS 在 Fedora 系统的安装方式暂未实现。"
                fi

                # 安装 qq 音乐
                if [ "$OS" = "debian" ] || [ "$OS" = "ubuntu" ] || [ "$OS" = "deepin" ]; then
                    # 这里可能需要从 GitHub 下载或者其他方式，暂未实现
                    echo "QQ 音乐的安装方式暂未实现。"
                elif [ "$OS" = "fedora" ]; then
                    echo "QQ 音乐的安装方式暂未实现。"
                fi
                ;;
            2)
                break
                ;;
            *)
                echo "无效选项，请重新输入。"
                ;;
        esac
    done
}

# 主菜单
main_menu() {
    while true; do
        echo ""
        echo "主菜单:"
        echo "1. 换源"
        echo "2. 打依赖与驱动"
        echo "3. 配置开发环境"
        echo "4. 安装解码器等"
        echo "5. 优化系统性能与安全设置"
        echo "6. 安装软件"
        echo "7. 退出"
        read -p "请输入选项: " main_choice

        case $main_choice in
            1)
                change_source
                ;;
            2)
                install_dependencies
                ;;
            3)
                configure_dev_env
                ;;
            4)
                install_codecs
                ;;
            5)
                optimize_system
                ;;
            6)
                install_software
                ;;
            7)
                echo "程序退出。"
                exit 0
                ;;
            *)
                echo "无效选项，请重新输入。"
                ;;
        esac
    done
}

# 启动主菜单
main_menu